<template>
  <el-dialog :title="title" :visible.sync="visiable" width="30%" :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="80px"  >
      <el-form-item label="分类名称:">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item align="center" >
        <el-button type="primary" @click="submit">确认</el-button>
        <el-button type="danger" @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import blogApi from '@/api/blog';
export default {
  props:{
    categoryId: null,  // 分类id
    title:{
      type:String,
      default:''
    },
    visiable:{  // 弹出窗口
      type:Boolean,
      default:false
    },
    formData: { // 提交表单数据
      type:Object,
      default:{}
    },
    remoteClose: Function, //用于关闭窗口
  },

  methods: {
    // 提交表单
    submit() {
      console.log('categoryId=' + this.categoryId);
      if (this.categoryId) {
        // 编辑提交
        blogApi.updateCategory(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          this.handleClose();
        })
      } else {
        // 新增提交
        blogApi.addCategory(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          this.handleClose();
        })
      }
    },

    // 关闭窗口
    handleClose() {
      // 调用父组件的方法关闭窗口
      this.formData.name = '';
      this.remoteClose();
    },
  }
}
</script>

<style scoped>

</style>
